<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="费用类型" prop="costType">
        <el-select v-model="queryParams.costType" placeholder="请选择费用类型" clearable>
          <el-option
            v-for="dict in dict.type.cost_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.people_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="费用编号" prop="costNum">
        <el-input
          v-model="queryParams.costNum"
          placeholder="请输入费用编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者id" prop="patientId">
        <el-select v-model="queryParams.patientId" placeholder="请选择患者id" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="费用状态" prop="costStatus">
        <el-select v-model="queryParams.costStatus" placeholder="请选择费用状态" clearable>
          <el-option
            v-for="dict in dict.type.patient_money_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cost:cost:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleAudit"
          v-hasPermi="['cost:cost:edit']"
        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="multiple"
          @click="handleCancel"
          v-hasPermi="['cost:cost:remove']"
        >取消</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cost:cost:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="costList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="费用类型" align="center" prop="costType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cost_type" :value="scope.row.costType"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.people_audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="费用编号" align="center" prop="costNum" />
      <el-table-column label="患者姓名" align="center" prop="register.name"/>
	  <el-table-column label="性别" align="center" prop="register.sex">
	    <template slot-scope="scope">
	      <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.register.sex"/>
	    </template>
	  </el-table-column>
	  <el-table-column label="年龄" align="center" prop="register.age"/>
	  <el-table-column label="手机" align="center" prop="register.phone"/>
	  <el-table-column label="预约科室" align="center" prop="register.departmentId" >
	  		  <template slot-scope="scope">
	  			  {{deparvalue[scope.row.register.departmentId]}}
	  		  </template>
	  </el-table-column>
	  <el-table-column label="预约医生" align="center" prop="register.doctorId" >
	  		  <template slot-scope="scope">
	  			  {{doctorvalue[scope.row.register.doctorId]}}
	  		  </template>
	  </el-table-column>
	  
      <el-table-column label="费用状态" align="center" prop="costStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.patient_money_status" :value="scope.row.costStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="费用" align="center" prop="money" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cost:cost:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cost:cost:remove']"
          >删除</el-button> -->
		  <el-button
		    size="mini"
		    type="text"
		    icon="el-icon-close"
		    @click="handleCancel(scope.row)"
		    v-hasPermi="['cost:cost:edit']"
		  >取消</el-button>
		  <el-button
		    size="mini"
		    type="text"
		    icon="el-icon-edit"
		    @click="handleAudit(scope.row)"
		    v-hasPermi="['cost:cost:remove']"
		  >审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改费用列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="费用类型" prop="costType">
          <el-select v-model="form.costType" placeholder="请选择费用类型">
            <el-option
              v-for="dict in dict.type.cost_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.people_audit_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用编号" prop="costNum">
          <el-input v-model="form.costNum" placeholder="请输入费用编号" />
        </el-form-item>
        <!-- <el-form-item label="患者id" prop="patientId">
          <el-select v-model="form.patientId" placeholder="请选择患者id">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="费用状态" prop="costStatus">
          <el-select v-model="form.costStatus" placeholder="请选择费用状态">
            <el-option
              v-for="dict in dict.type.patient_money_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用" prop="money">
          <el-input v-model="form.money" placeholder="请输入费用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCost, getCost, delCost, addCost, updateCost } from "@/api/cost/cost";
import { listDepartment} from "@/api/department/department";
	import { listDoctor} from "@/api/doctor/doctor";
export default {
  name: "Cost",
  dicts: ['patient_money_status', 'cost_type', 'sys_user_sex', 'people_audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 费用列表表格数据
      costList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        costType: null,
        auditStatus: null,
        costNum: null,
        patientId: null,
        costStatus: null,
      },
      // 表单参数
      form: {},
	  deparvalue:[],
	  doctorvalue:[],
      // 表单校验
      rules: {
        costNum: [
          { required: true, message: "费用编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
	this.getDepartmentList();
	this.getDoctorList();
  },
  methods: {
	  //	取消审核
	  handleCancel(row){
		  row.auditStatus = 1
		  let id = row.id
		  let auditStatus = row.auditStatus
		  return updateCost({id, auditStatus})
		    .then(response => {
		    })
		    .catch(() => {
		      this.$modal.msgError("操作失败");
		    });
	  },
	  handleAudit(row){
		row.auditStatus = 0  
		let id = row.id
		let auditStatus = row.auditStatus
		return updateCost({id, auditStatus})
		  .then(response => {
		  })
		  .catch(() => {
		    this.$modal.msgError("操作失败");
		  });
	  },
	  // 查询科室列表
	  getDepartmentList(){
	  		  listDepartment().then(response=>{
	  			  for(var i=0;i<response.rows.length;i++){
	  			  		this.$set(this.deparvalue, response.rows[i].id,response.rows[i].name);
	  				}
	  			  		  // console.log(this.deparvalue);
	  		  });
	  },
	  // 查询医生列表
	  getDoctorList(){
	  		  listDoctor().then(response=>{
	  			  for(var i=0;i<response.rows.length;i++){
	  					this.$set(this.doctorvalue, response.rows[i].id,response.rows[i].name);
	  				}
	  					 // console.log(this.doctorvalue);
	  		  });
	  },
	  
    /** 查询费用列表列表 */
    getList() {
      this.loading = true;
      listCost(this.queryParams).then(response => {
        this.costList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        costType: null,
        auditStatus: null,
        costNum: null,
        patientId: null,
        costStatus: null,
        money: null,
        createTime: null,
        del: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加费用列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCost(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改费用列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除费用列表编号为"' + ids + '"的数据项？').then(function() {
        return delCost(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cost/cost/export', {
        ...this.queryParams
      }, `cost_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
